import sympy as sp

def derivative_reference_table():
    x = sp.Symbol('x')
    a = sp.Symbol('a', constant=True)  # 常数底数
    n = sp.Symbol('n', constant=True)  # 常数指数
    
    # 基本函数导数公式 - 直接使用SymPy函数而不是字符串
    functions = [
        ("常数函数", sp.Symbol('C'), 0),
        ("幂函数", x**n, n*x**(n-1)),
        ("自然指数函数", sp.exp(x), sp.exp(x)),
        ("一般指数函数", a**x, a**x * sp.log(a)),
        ("自然对数函数", sp.log(x), 1/x),
        ("一般对数函数", sp.log(x, a), 1/(x * sp.log(a))),
        ("正弦函数", sp.sin(x), sp.cos(x)),
        ("余弦函数", sp.cos(x), -sp.sin(x)),
        ("正切函数", sp.tan(x), 1/sp.cos(x)**2),
        ("余切函数", sp.cot(x), -1/sp.sin(x)**2),
        ("反正弦函数", sp.asin(x), 1/sp.sqrt(1-x**2)),
        ("反余弦函数", sp.acos(x), -1/sp.sqrt(1-x**2)),
        ("反正切函数", sp.atan(x), 1/(1+x**2))
    ]
    
    print("基本初等函数导数公式速查表")
    print("=" * 70)
    print(f"{'函数类型':<12} {'函数表达式':<15} {'导数公式':<30} {'Python验证'}")
    print("-" * 70)
    
    for func_type, func, deriv_formula in functions:
        # 直接计算导数
        deriv_calculated = sp.diff(func, x)
        
        # 确保导数被实际计算
        if hasattr(deriv_calculated, 'doit'):
            deriv_calculated = deriv_calculated.doit()
        
        # 简化计算结果
        deriv_calculated_simplified = sp.simplify(deriv_calculated)
        
        # 检查公式是否正确
        is_correct = False
        
        # 方法1: 直接比较
        if deriv_calculated_simplified == deriv_formula:
            is_correct = True
        else:
            # 方法2: 尝试简化后比较
            simplified_formula = sp.simplify(deriv_formula)
            if deriv_calculated_simplified == simplified_formula:
                is_correct = True
            else:
                # 方法3: 检查数学等价性
                difference = sp.simplify(deriv_calculated_simplified - simplified_formula)
                is_correct = (difference == 0)
        
        # 根据验证结果选择显示方式
        if is_correct:
            verification = "✓"
        else:
            # 显示SymPy计算结果和期望公式
            verification = f"{deriv_calculated_simplified} || {deriv_formula}"
        
        # 获取函数表达式的字符串表示
        if func_type == "常数函数":
            func_str = "C"
        elif func_type == "幂函数":
            func_str = "x^n"
        elif func_type == "一般指数函数":
            func_str = "a^x"
        elif func_type == "一般对数函数":
            func_str = "log_a(x)"
        else:
            func_str = str(func).replace('**', '^').replace('*', '')
        
        print(f"{func_type:<12} {func_str:<15} {str(deriv_formula):<30} {verification}")
    
    return functions

# 调用公式表
print("导数公式表验证结果：")
formulas = derivative_reference_table()

# 补充：导数的四则运算法则表
def operations_reference_table():
    print("\n导数的四则运算法则")
    print("=" * 50)
    
    operations = [
        ("和差法则", "[f(x) ± g(x)]'", "f'(x) ± g'(x)"),
        ("乘法法则", "[f(x)g(x)]'", "f'(x)g(x) + f(x)g'(x)"),
        ("数乘法则", "[cf(x)]'", "cf'(x)"),
        ("除法法则", "[f(x)/g(x)]'", "[f'(x)g(x) - f(x)g'(x)] / [g(x)]²")
    ]
    
    for name, expr, rule in operations:
        print(f"{name:<10} {expr:<25} {rule}")

operations_reference_table()